package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.DataOverViewQueryDTO;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 目标：
 *
 * @author Ashely
 * @date 2024/6/18 9:52
 */
@Mapper
public interface OrderMapper {
    @Insert("insert into orders\n" +
            "        (number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark,\n" +
            "         phone, address, consignee, estimated_delivery_time, delivery_status, pack_amount, tableware_number,\n" +
            "         tableware_status)\n" +
            "        values (#{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod},\n" +
            "                #{payStatus}, #{amount}, #{remark}, #{phone}, #{address}, #{consignee},\n" +
            "                #{estimatedDeliveryTime}, #{deliveryStatus}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Orders order);
@Select("select * from orders where number = #{number} and user_id = #{userId}")
    Orders getByNumber(String number,Long userId);

    void update(Orders orders);
    @Select("select * from orders where id = #{id}")
    OrderVO getById(Long id);
//    @Select("select d.name,d.dish_id from order_detail d left join orders o on d.order_id=o.id where o.status=5 and o.order_time between #{begin} and #{end}")
//    List<OrderDetail> find(String begin, String end);
    Page<OrderVO> page(OrdersPageQueryDTO ordersPageQueryDTO);

    Integer countStatus(Integer status);
    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndOrdertimeLT(Integer status, LocalDateTime orderTime);
@Select("select count(id) from orders where order_time between #{begin} and #{end}")
Integer getOrderCompletion(LocalDateTime begin, LocalDateTime end);
@Select("select sum(amount) from orders where order_time between #{begin} and #{end} and status = #{status}")
    Double getTurnover(LocalDateTime begin, LocalDateTime end,Integer status);
@Select("select count(id) from orders where order_time between #{begin} and #{end} and status=#{status}")
    Integer getCompletion(LocalDateTime begin, LocalDateTime end, Integer status);
@Select("select count(*) from (select count(*) from orders where order_time between #{begin} and #{end} and status=#{status} group by user_id) tb")
    Double getOrderUserCount(LocalDateTime begin, LocalDateTime end, Integer status);


    Double sumByMap(Map map);

    Integer countByMap(Map map);

    List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);
}

